package com.start.boot.web;

import com.start.boot.common.Param_Pager;
import com.start.boot.common.SystemConfiguration;
import com.start.boot.dao.ajpc.YX_PC_JBXXMapper;
import com.start.boot.dao.ajpc.Yx_Pc_PcxFlMapper;
import com.start.boot.dao.ajpc.Yx_Pc_PcxMapper;
import com.start.boot.domain.*;
import com.start.boot.pojo.vo.PcxVoList;
import com.start.boot.pojo.vo.Yx_Pc_PcxFlVo;
import com.start.boot.service.FilterService;
import com.start.boot.support.utils.EasyUIHelper;
import com.start.boot.support.utils.FastJsonUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

/**
 * 筛选控制器
 * Created by lei on 2017/11/2.
 */
@RestController
@RequestMapping("/filter")
public class FilterController extends ArchivesSystemBaseController{

    @Autowired
    private FilterService filterService;
    @Autowired
    Yx_Pc_PcxMapper yx_pc_pcxMapper;

    @Autowired
    Yx_Pc_PcxFlMapper yx_pc_pcxFlMapper;

    @Autowired
    YX_PC_JBXXMapper yx_pc_jbxxMapper;



    /**
     * 评查筛选规则列表
     */
    @RequestMapping("/getSxgz")
    public String getSxgz(String pcflbm, String sslb) {

        //响应到页面封装
        String result = "";

        try {
            List<Map> list = filterService.getSxgz(SystemConfiguration.djdwbm, pcflbm, sslb);

            result = EasyUIHelper.buildTreeDataSourceWithoutIconCol(list, "GZBM", "FGZBM", "GZMC", "-1");
        } catch (Exception e) {
            super.errMsg("评查筛选规则列表获取失败", pcflbm, e);
        }

        return result;
    }

    /**
     * 评查活动绑定的筛选规则列表
     */
    @RequestMapping("/getHdsxgz")
    public String getHdsxgz(String pcflbm, String pchdbm) {

        //响应到页面封装
        String result = "";

        try {
            List<Map> list = filterService.getHdsxgz(SystemConfiguration.djdwbm, pcflbm, pchdbm);

            result = EasyUIHelper.buildTreeDataSourceWithoutIconCol(list, "GZBM", "FGZBM", "GZMC", "-1");
        } catch (Exception e) {
            super.errMsg("评查活动绑定的筛选规则列表获取失败", pchdbm, e);
        }

        return result;
    }

    /**
     * 随机评查案件筛选（自定义）
     */
    @RequestMapping("/getSjsx")
    public String getSjsx(String json) {

        //响应到页面封装
        String result = "";

        try {
            ParamSx param = FastJsonUtils.toObject(ParamSx.class, json);
            param.setGzdwbm(SystemConfiguration.djdwbm);
            param.setPage(parsePage(getParameter("page")));
            param.setRows(parseRows(getParameter("rows")));

            ParamSx data = filterService.getSjsx(param);

            result = EasyUIHelper.buildDataGridDataSource(data.getList(), data.getCount());
        } catch (Exception e) {
            super.errMsg("随机评查案件筛选（自定义）获取失败", json, e);
            result= failure(e.getMessage(),"随机评查案件筛选（部门）获取失败");
        }

        return result;
    }

    /**
     * 随机评查案件筛选（部门）
     */
    @RequestMapping("/get_sjsx_bm")
    public String get_sjsx_bm(String json) {

        //响应到页面封装
        String result = "";

        try {
            ParamSx param = FastJsonUtils.toObject(ParamSx.class, json);

            ParamSx data = filterService.get_sjsx_bm(param);
            result = success(data, "随机评查案件筛选（部门)成功");
        } catch (Exception e) {
            super.errMsg("随机评查案件筛选（部门）获取失败",json,e);
            result= failure(e.getMessage(),"随机评查案件筛选（部门）获取失败");
        }

        return result;
    }

    /**
     * 随机评查案件筛选（检察官）
     */
    @RequestMapping("/get_sjsx_jcg")
    public String get_sjsx_jcg(String json) {

        //响应到页面封装
        String result = "";

        try {
            ParamSx param = FastJsonUtils.toObject(ParamSx.class, json);
            //param.setPage(parsePage(getParameter("page")));
            //param.setRows(parseRows(getParameter("rows")));

            ParamSx data = filterService.get_sjsx_jcg(param);
            result = success(data, "随机评查案件筛选（检察官)成功");
        } catch (Exception e) {
            super.errMsg("随机评查案件筛选（检察官）获取失败",json,e);
            result = failure(e.getMessage(), "随机评查案件筛选（检察官）获取失败");
        }

        return result;
    }

    /**
     * 评查监控
     */
    @RequestMapping("/getPcjk")
    public String getPcjk(String json){

        //响应到页面封装
        String result = "";

        try {
            Param_Pcjk pcjkParam = FastJsonUtils.toObject(Param_Pcjk.class, json);
            pcjkParam.setDwbm(getCurrentDwbm());
            pcjkParam.setGh(getCurrentGh());
            pcjkParam.setPage(parsePage(getParameter("page")));
            pcjkParam.setRows(parseRows(getParameter("rows")));

            Param_Pager data = filterService.getPcjk(pcjkParam);
            result = EasyUIHelper.buildDataGridDataSource(data.getList(), data.getCount());
        }catch (Exception e){
            super.errMsg("评查监控获取失败",json,e);
            result = failure(e.getMessage(), "获取评查监控失败");
        }

        return result;
    }

    /**
     * 获取评查结论
     */
    @RequestMapping("/getPcjl")
    public String getPcjl(){

        //响应到页面封装
        String result = "";

        try {
            List<Map> list = filterService.getPcjl();
            result = EasyUIHelper.buildTreeDataSourceWithoutIconCol(list, "PCJLBM", "FBM", "PCJL", "-1");
        } catch (Exception e) {
            super.errMsg("获取评查结论失败",null,e);
            result = failure(e.getMessage(), "获取评查结论失败");
        }

        return result;
    }

    /**
     * 获取评查状态
     */
    @RequestMapping("/getPczt")
    public String getPczt(){

        //响应到页面封装
        String result = "";

        try {
            List<Map> list = filterService.getPczt();
            result = EasyUIHelper.buildTreeDataSourceWithoutIconCol(list, "PCZTBM", "FBM", "PCZT", "-1");
        } catch (Exception e) {
            super.errMsg("获取评查状态失败",null,e);
            result = failure(e.getMessage(), "获取评查状态失败");
        }

        return result;
    }

    // 部门已经评查/未评查列表
    @RequestMapping("/getBmwpcList")
    public String getBmwpcList(String json){

        //响应到页面封装
        String result = "";

        try {
            ParamSx param = FastJsonUtils.toObject(ParamSx.class, json);
            param.setPage(parsePage(getParameter("page")));
            param.setRows(parseRows(getParameter("rows")));
            if (StringUtils.isEmpty(param.getCbdwbm())){
                param.setCbdwbm(getCurrentDwbm());
            }

            Param_Pager data = filterService.getBmwpcList(param);
            result = EasyUIHelper.buildDataGridDataSource(data.getList(), data.getCount());
        }catch (Exception e){
            super.errMsg("部门已经评查/未评查列表获取失败",json,e);
            result=  failure(e.getMessage(),"部门已经评查/未评查列表获取失败");
        }

        return result;
    }

    // 检察官已经评查/未评查列表
    @RequestMapping("/getCbrwpcList")
    public String getCbrwpcList(String json){

        //响应到页面封装
        String result = "";

        try {
            ParamSx param = FastJsonUtils.toObject(ParamSx.class, json);
            param.setPage(parsePage(getParameter("page")));
            param.setRows(parseRows(getParameter("rows")));
            if (StringUtils.isEmpty(param.getCbdwbm())){
                param.setCbdwbm(getCurrentDwbm());
            }

            Param_Pager data = filterService.getCbrwpcList(param);
            result = EasyUIHelper.buildDataGridDataSource(data.getList(), data.getCount());
        }catch (Exception e){
            super.errMsg("检察官已经评查/未评查列表获取失败",json,e);
            result=  failure(e.getMessage(),"检察官已经评查/未评查列表获取失败");
        }

        return result;
    }

    // 随机、重点评查
    @RequestMapping("/uptSjpcsx")
    public String uptSjpcsx(String json){
        String result = "";

        try {
            Param_Ajsx param = FastJsonUtils.toObject(Param_Ajsx.class, json);

            param.setPcdwbm(getCurrentDwbm());
            param.setCzr_dwbm(getCurrentDwbm());
            param.setCzr_dwmc(getCurrentDwmc());
            param.setCzr_gh(getCurrentGh());
            param.setCzr_mc(getCurrentMC());
            if (StringUtils.isEmpty(param.getPcdwbm())){
                param.setPcr_dwbm(getCurrentDwbm());
                param.setPcr_dwmc(getCurrentDwmc());
                param.setPcr_gh(getCurrentGh());
                param.setPcr_mc(getCurrentMC());
            }
            param.setPcsah("");
            param.setPcslbm("");
            param = filterService.uptSjpcsx(param);
            result = success(param, "随机/重点筛选案件成功");
        }catch (Exception e){
            super.errMsg("随机/重点筛选案件失败", json, e);
            result = failure(e.getMessage(), "随机/重点筛选案件失败");
        }

        return result;
    }

    // 专项评查
    @RequestMapping("/uptZxpcsx")
    public String uptZxpcsx(String json){
        String result = "";

        try {
            Param_Ajsx param = FastJsonUtils.toObject(Param_Ajsx.class, json);

            param.setPcdwbm(getCurrentDwbm());
            param.setCzr_dwbm(getCurrentDwbm());
            param.setCzr_dwmc(getCurrentDwmc());
            param.setCzr_gh(getCurrentGh());
            param.setCzr_mc(getCurrentMC());
            param.setPcsah("");
            param.setPcslbm("");
            param = filterService.uptZxpcsx(param);
            result = success(param, "专项评查筛选案件成功");
        }catch (Exception e){
            super.errMsg("专项评查筛选案件失败", json, e);
            result = failure(e.getMessage(), "专项评查筛选案件失败");
        }

        return result;
    }

    @ResponseBody
    @RequestMapping("/getEvalCards")
    public Object getEvalCards(String pcslbm,String pcxflbm) {

        try {
            PcxVoList yxPcFl = filterService.getYxPcFl(pcslbm, pcxflbm);
            return yxPcFl;
        } catch (Exception e) {
            super.errMsg("获取评查案卡数据失败", pcslbm, e);
        }

        return null;
    }

    @ResponseBody
    @RequestMapping("/saveEvalCards")
    public String saveEvalCards(String json, String jbxx) {
        String result = "";

        try {
            PcxVoList param = FastJsonUtils.toObject(PcxVoList.class, json);
            List<Yx_Pc_PcxFlVo> pcxFlVos = param.getPcxFlVos();
            if (pcxFlVos.isEmpty()) {
                return failure("400", "提交数据为空，保存失败");
            }
            pcxFlVos.stream().forEach(data -> {
                try {
                    update(data);
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            });

            YX_PC_JBXX pcxx = FastJsonUtils.toObject(YX_PC_JBXX.class, jbxx);
            yx_pc_jbxxMapper.updateByPrimaryKey(pcxx);

            result = success(param, "保存评查案卡数据成功");

        } catch (Exception e) {
            super.errMsg("保存评查案卡数据失败", json, e);
            result = failure(e.getMessage(), "保存评查案卡数据失败");
        }
        return result;
    }

    private void update(Yx_Pc_PcxFlVo vo) throws InvocationTargetException, IllegalAccessException {
        Yx_Pc_PcxFl yx_pc_pcxFl = new Yx_Pc_PcxFl();
        BeanUtils.copyProperties(vo, yx_pc_pcxFl);
        yx_pc_pcxFlMapper.updateByPrimaryKeySelective(yx_pc_pcxFl);
        List<Yx_Pc_Pcx> pcxList = vo.getPcxList();
        if (!pcxList.isEmpty()) {
            pcxList.stream().forEach(pcx -> {
                yx_pc_pcxMapper.updateByPrimaryKeySelective(pcx);
            });
        }
        List<Yx_Pc_PcxFlVo> children = vo.getChildren();
        if (!children.isEmpty()) {
            children.stream().forEach(data -> {
                try {
                    update(data);
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            });
        }
    }
    @RequestMapping("/updateJbxx")
    public String updateJBxx(YX_PC_JBXX jbxx){
        if (jbxx==null){
            return failure("400","提交数据为空，修改失败");
        }
        try{
            yx_pc_jbxxMapper.updateByPrimaryKeySelective(jbxx);
            return success("200","修改成功");
        }catch (Exception e){
            e.printStackTrace();
            return failure("400","修改失败，修改失败");
        }
    }


    /**
     * 获取部门编码
     * @param
     * @return
     */
    @RequestMapping("/getBmList")
    public String getBmList(String pcflbm) {
        //响应到页面封装
        String result = "";

        String dwbm = getCurrentDwbm();

        try {
            List<Map> bmList = filterService.getBmList(dwbm,pcflbm);
            result = EasyUIHelper.buildTreeDataSourceWithoutIconCol(bmList, "BMBM", "FBMBM", "BMMC", "-1");

        } catch (Exception e) {
            e.printStackTrace();
            super.errMsg("获取部门编码失败",dwbm,e);
        }

        return result;

    }

}
